<template>
	<view>
		<uni-section title="选择时间" type="line" style="height: 100vh;">
			<uni-card>
				<view class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-db">
							<picker mode="time" :value="time" start="00:00" end="23:59" @change="bindTimeChange">
								<view class="uni-input">{{ time }}</view>
							</picker>
						</view>
					</view>
				</view>

			</uni-card>

			<uni-card>
				<view @click="inputDialogToggle" style="font-size: 30rpx;">重复</view>

			</uni-card>

			<button style="margin: 0 auto;" class="button popup-success" @click="messageToggle('success')"><text
					class="button-text success-text">保存</text></button>

		</uni-section>

		<uni-popup ref="inputDialog" type="dialog">
			<view class="uni-list" style="width: 130px;border-radius: 10px;">
				<checkbox-group v-model="selectedDays">
					<label class="uni-list-cell uni-list-cell-pd" v-for="day in days" :key="day">
						<view>
							<checkbox :value="day" :checked="selectedDays.includes(day)"
								@change="onCheckboxChange(day)" />
						</view>
						<view>{{ day }}</view>
					</label>
				</checkbox-group>
			</view>
		</uni-popup>
		<view>
			<!-- 提示信息弹窗 -->
			<uni-popup ref="message" >
				<uni-popup-message :type="msgType" message="保存成功" :duration="2000"></uni-popup-message>
			</uni-popup>
		</view>
	</view>

	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			});
			return {
				type: 'center',
				date: currentDate,
				time: '12:01',
				seconds: 0,
				timeInit: 0,
				msgType: 'success',
				messageText: '这是一条成功提示',
				days: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
				selectedDays: ['周一', '周二'],
			};
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		created() {
			const currentDate = new Date();
			const hours = currentDate.getHours();
			const minutes = currentDate.getMinutes();
			const formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
			this.date = currentDate;
			this.time = formattedTime;
		},
		methods: {
			bindTimeChange(e) {
				this.time = e.detail.value;
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			inputDialogToggle() {
				this.$refs.inputDialog.open()
			},
			dialogInputConfirm(val) {
				uni.showLoading({
					title: '3秒后会关闭'
				})

				setTimeout(() => {
					uni.hideLoading()
					console.log(val)
					this.value = val
					// 关闭窗口后，恢复默认内容
					this.$refs.inputDialog.close()
				}, 3000)
			},

			messageToggle(type) {
				this.msgType = type
				this.messageText = `这是一条${type}消息提示`
				this.$refs.message.open()
				uni.showToast({
					title: '保存成功',
					icon: '',
					duration: 350,
					mask: true
				})
				
				// 返回上个页面
				setTimeout(() => {
					uni.navigateBack({
						delta: 1
					})
				}, 400)
			},
			onCheckboxChange(day) {
				if (this.selectedDays.includes(day)) {

					const index = this.selectedDays.indexOf(day);
					if (index !== -1) {
						this.selectedDays.splice(index, 1);
					}
				} else {

					this.selectedDays.push(day);
				}
			}

		}
	};
</script>

<style scoped>
	.uni-common-pl {
		padding-left: 30rpx;
	}

	.uni-title text {
		font-size: 24rpx;
		color: #888;
	}

	.uni-list-cell-left {
		white-space: nowrap;
		font-size: 40rpx;
		padding: 0 30rpx;
	}

	.uni-list {
		background-color: #FFFFFF;
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.uni-list-cell {
		position: relative;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.uni-list:after {
		position: absolute;
		z-index: 10;
		right: 0;
		bottom: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		/* background-color: #c8c7cc; */
	}

	.uni-list-cell-pd {
		padding: 22rpx 30rpx;
	}

	.uni-list-cell-db,
	.uni-list-cell-right {
		flex: 1;
	}

	.uni-card {
		cursor: pointer;

		padding: 10px;

	}

	.selected-days {
		font-weight: bold;
		margin-left: 10px;
	}

	.uni-input {
		font-size: 40rpx;
	}

	.repeat-label {
		font-weight: bold;
		margin-right: 10px;
		cursor: pointer;
	}

	.button {

		align-items: center;
		justify-content: center;
		flex: 1;
		height: 50px;
		margin: 0 5px;
		width: 100px;
		border-radius: 5px;
		bottom: 20px;
		position: fixed;
		left: 40%;
		/* 水平居中 */
	}

	.popup-success {
		color: #fff;
		background-color: #007BFF;
	}

	.button-text {
		color: #fff;
		font-size: 12px;
	}

	.success-text {
		color: #ffffff;
		font-size: 20px;

	}
</style>